package com.gec.system.front.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.system.exception.MyCustomException;
import com.gec.system.front.FrontUser;
import com.gec.system.front.service.FrontUserService;
import com.gec.system.service.SysMovieService;
import com.gec.system.system.SysMovie;
import com.gec.system.util.JwtHelper;
import com.gec.system.util.MDHelper;
import com.gec.system.util.Result;
import com.gec.system.vo.FrontUserVo;
import com.gec.system.vo.SysQueryMovieVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-09-21
 */
@Slf4j
@RestController
@Api(tags = "门户管理控制器")
@RequestMapping("/admin/front/user")
public class FrontUserController {
    @Autowired
    FrontUserService frontUserService;
    @Autowired
    SysMovieService movieService;
    @PostMapping("login")
    public Result login(@RequestBody FrontUserVo vo){
        FrontUser name = frontUserService.getFrontUserName(vo.getUsername());
        if (name==null){
            throw new MyCustomException(20001,"用户不存在");
        }
        String password = vo.getPassword();
        password = MDHelper.encrypt(password);
        if (!name.getPassword().equals(password)){
            throw new MyCustomException(20002,"密码错误");
        }
        if (name.getStatus()==0){
            throw new MyCustomException(20003,"账号被停用..");

        }
        String token = JwtHelper.createToken(name.getId() + "", name.getUsername());
        Map<String,Object> map =new HashMap<>();
        map.put("token",token);
        map.put("user",name);
        return Result.ok(map);
    }
    @PostMapping("registered")
    public Result registered(@RequestBody FrontUser frontUser){
        String password = MDHelper.encrypt(frontUser.getPassword());
        frontUser.setPassword(password);
        frontUser.setLevel(1L);
        frontUser.setLevellimit(10L);
        frontUser.setSmoney(0L);
        String token = JwtHelper.createToken(frontUser.getId() + "", frontUser.getUsername());
        boolean save = frontUserService.save(frontUser);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",frontUser);
        map.put("save",save?Result.ok():Result.fail());
        return Result.ok(map);
    }
    @GetMapping("movie/{page}/{limit}")
    public Result getMovie(@PathVariable("page") Long pageIndex, @PathVariable Long limit , SysQueryMovieVo vo){
        Page<SysMovie> page = new Page<>(pageIndex,limit);
        IPage<SysMovie> sysMovieIPage = movieService.selectPage(page, vo);
        return Result.ok(sysMovieIPage);
    }
    @GetMapping("/movie/count")
    public Result count(){
        int count = movieService.count();
        return Result.ok(count);
    }

        @ApiOperation("info方法")
    @GetMapping("/info")
    public Result info(HttpServletRequest request){
        if(request.getHeader("token")==null){
            log.warn(request.getHeader("token"));
            return null;
        }
        String token = request.getHeader("token");
        String username = JwtHelper.getUsername(token);
        Map<String,Object> map =  frontUserService.getUserInfo(username);
        System.out.print(map);
        return Result.ok(map);
    }
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }



}




